package com.cn.common.utils;

import com.cn.common.domain.Tree;
import com.cn.system.domain.DeptDO;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

public class BuildTree {

    public static <T> Tree<T> build(List<Tree<T>> nodes) {

        if (nodes == null) {
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();

        for (Tree<T> children : nodes) {

            String pid = children.getParentId();
            if (pid == null || "0".equals(pid)) {
                topNodes.add(children);

                continue;
            }

            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildren().add(children);
                    children.setHasParent(true);
                    parent.setHasChildren(true);
                    continue;
                }
            }

        }

        Tree<T> root = new Tree<>();
        if (topNodes.size() == 1) {
            root = topNodes.get(0);
        } else {
            root.setId("-1");
            root.setParentId("");
            root.setHasParent(false);
            root.setHasChildren(true);
            root.setChecked(true);
            root.setChildren(topNodes);
            root.setText("顶级节点");
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            root.setState(state);
        }

        return root;
    }

    public static <T> List<Tree<T>> buildList(List<Tree<T>> nodes, String idParam) {
        if (nodes == null) {
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            if (pid == null || idParam.equals(pid)) {
                topNodes.add(children);
                continue;
            }
            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildren().add(children);
                    children.setHasParent(true);
                    parent.setHasChildren(true);
                    continue;
                }
            }

        }
        return topNodes;
    }

    public static <T> Tree<T> build4Depth(List<Tree<T>> nodes, String idParam) {
        List<Tree<T>> trees = buildList4Depth(nodes, idParam);
        Tree<T> root;
        if (trees.size() == 1) {
            root = trees.get(0);
        } else {
            root = new Tree<>();
            root.setId("0");
            root.setParentId("-1");
            root.setHasParent(false);
            root.setHasChildren(true);
            root.setChecked(true);
            root.setChildren(trees);
            root.setText("顶级节点");
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            root.setState(state);
            root.setChildren(trees);
        }
        return root;
    }

    public static <T> List<Tree<T>> buildList4Depth(List<Tree<T>> nodes, String idParam) {
        if (nodes == null) {
            return null;
        }
        Queue<Tree> queue = new LinkedBlockingQueue<>();
        List<Tree<T>> root = new ArrayList<>();
        nodes.stream().filter(s -> idParam.equalsIgnoreCase(s.getParentId())).forEach(t -> {
            queue.add(t);
            t.setHasParent(true);
            root.add(t);
        });
        while (!queue.isEmpty()) {
            Tree<T> parent = queue.poll();
            nodes.stream().filter(t -> parent.getId().equalsIgnoreCase(t.getParentId())).forEach(t -> {
                t.setHasParent(true);
                parent.setHasChildren(true);
                parent.getChildren().add(t);
                queue.add(t);
            });
        }
        return root;
    }

}